package com.colin.base.javaconcurrency.sixstates;

/**
 * Created by Colin on 2020/3/3 20:09
 * email: colinzhaodong@gmail.com
 * desc: 展示Blocked, Waiting, TimedWaiting
 *
 * @author zhaod
 */
public class BlockedWaitingTimedWaiting implements Runnable{
	public static void main(String[] args) {
		BlockedWaitingTimedWaiting runnable = new BlockedWaitingTimedWaiting();
		Thread thread1 = new Thread(runnable);
		thread1.start();
		Thread thread2 = new Thread(runnable);
		thread2.start();
		try {
			Thread.sleep(5);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(thread1.getState());
		//打印出BLOCKED状态，因为thread2想拿得到sync()的锁却拿不到
		System.out.println(thread2.getState());
		try {
			Thread.sleep(1300);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		//打印出WAITING状态，因为执行了wait()
		System.out.println(thread1.getState());
		//wait()会释放synchronize锁
		try {
			Thread.sleep(1300);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(thread2.getState());
	}
	@Override
	public void run() {
		syn();
	}
	private synchronized void syn() {
		try {
			Thread.sleep(1000);
			wait();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
